#!/usr/bin/perl
#Programmer: Rory Carmichael
#Purpose: Translate a given multifasta file in all first reading frame, making no attempt to distinguish start codons or any such nonsense
#NOTE: uncommenting stuff will make it work on all 6 reading frames
#Usage: blind_translate.pl <multifastafile>  (reads from stdin)

use strict;

my %DNAtoAA = ('GCT' => 'A', 'GCC' => 'A', 'GCA' => 'A', 'GCG' => 'A', 'TGT' => 'C',
	       'TGC' => 'C', 'GAT' => 'D', 'GAC' => 'D', 'GAA' => 'E', 'GAG' => 'E',
	       'TTT' => 'F', 'TTC' => 'F', 'GGT' => 'G', 'GGC' => 'G', 'GGA' => 'G',
	       'GGG' => 'G', 'CAT' => 'H', 'CAC' => 'H', 'ATT' => 'I', 'ATC' => 'I',
	       'ATA' => 'I', 'AAA' => 'K', 'AAG' => 'K', 'TTG' => 'L', 'TTA' => 'L',
	       'CTT' => 'L', 'CTC' => 'L', 'CTA' => 'L', 'CTG' => 'L', 'ATG' => 'M',
	       'AAT' => 'N', 'AAC' => 'N', 'CCT' => 'P', 'CCC' => 'P', 'CCA' => 'P',
	       'CCG' => 'P', 'CAA' => 'Q', 'CAG' => 'Q', 'CGT' => 'R', 'CGC' => 'R',
	       'CGA' => 'R', 'CGG' => 'R', 'AGA' => 'R', 'AGG' => 'R', 'TCT' => 'S',
	       'TCC' => 'S', 'TCA' => 'S', 'TCG' => 'S', 'AGT' => 'S', 'AGC' => 'S',
	       'ACT' => 'T', 'ACC' => 'T', 'ACA' => 'T', 'ACG' => 'T', 'GTT' => 'V',
	       'GTC' => 'V', 'GTA' => 'V', 'GTG' => 'V', 'TGG' => 'W', 'TAT' => 'Y',
	       'TAC' => 'Y', 'TAA' => '*', 'TAG' => '*', 'TGA' => '*');

sub translate_seq {
	my ($pos,$seq) = @_;
	my $retval;
	for(my $i = $pos; $i < (length($seq) - 2); $i+=3) {
		$retval .= $DNAtoAA{substr($seq,$i,3)};
	}
	return $retval;
}

sub rev_comp {
	my ($seq) = @_;
	$seq =~ tr/ATCG/TAGC/;
	$seq = reverse($seq);
	return $seq;
}

sub max_orf_len {
	my ($seq) = @_;
	my @orfs = split(/\*/, $seq);
	my $max = 0;
	foreach my $orf (@orfs) {
		if (length($orf) > $max) {
			$max = length($orf);
		}
	}
	return $max;
}

my $curtitle;
my $curseq;
while(my $line = <>) {
	chomp $line;
	if ($line =~ /^>/) {
		$curtitle = $line;
	} elsif (defined($curtitle)) {
		my @frames;
		$curseq = $line;
		$curseq =~ tr/acgt/ACGT/;

#		print $curtitle . "\n";
#		print &translate_seq(0,$curseq) . "\n";
#		print $curtitle . " 1st Reading Frame\n";
		print "$curtitle\n";
		print &translate_seq(0,$curseq) . "\n";
#		$frames[0] = &translate_seq(0,$curseq) . "\n";

#		print $curtitle . " 2nd Reading Frame\n";
#		print &translate_seq(1,$curseq) . "\n";
#		$frames[1] = &translate_seq(1,$curseq) . "\n";
#		print $curtitle . " 3rd Reading Frame\n";
#		print &translate_seq(2,$curseq) . "\n";
#		$frames[2] = &translate_seq(2,$curseq) . "\n";

#		$curseq = &rev_comp($curseq);
#		print $curtitle . " 4th Reading Frame\n";
#		print &translate_seq(0,$curseq) . "\n";;
#		$frames[3] = &translate_seq(0,$curseq) . "\n";;
#		print $curtitle . " 5th Reading Frame\n";
#		print &translate_seq(1,$curseq) . "\n";
#		$frames[4] = &translate_seq(1,$curseq) . "\n";
#		print $curtitle . " 6th Reading Frame\n";
#		print &translate_seq(2,$curseq) . "\n";
#		$frames[5] = &translate_seq(2,$curseq) . "\n";
#		my $max = 0;
#		my $max_i = 0;
#		for(my $i=0;$i<6;$i++) {
#			my $cur_len = &max_orf_len($frames[$i]);
##			print STDERR "$cur_len\n";
#			if($cur_len > $max) {
#				$max =  $cur_len;
#				$max_i = $i;
#			}
#		}
#		my $frame = $max_i + 1;
#		print $curtitle . " Reading Frame: $frame Length: $max\n";
#		print $frames[$max_i];
	}
}



